let form = layui.form;
function getArtData() {
    $.ajax({
        type: "get",
        url: "/my/article/cates",
        success: function (res) {
            if (res.status !== 0) return layer.msg(res.message);
            // console.log(res);
            // layer.msg(res.message);
            // console.log(res.data);
            let content = template("tem", res);
            // console.log(content);
            $("tbody").html(content)
        }
    })
}
getArtData();
var index_add = null;
$("#add_cont").on("click", function () {
    index_add = layer.open({
        type: 1,
        area: ['500px', '250px'],
        title: '添加文章分类',
        content: $("#sub_cont").html()
    })
})
$("body").on("submit", "#form-add", function (e) {
    e.preventDefault();
    $.ajax({
        type: "post",
        url: "/my/article/addcates",
        data: $(this).serialize(),
        success: function (res) {
            console.log(res);
            if (res.status !== 0) {
                return layer.msg('新增分类失败！')
            }
            getArtData()
            layer.msg('新增分类成功！')
            layer.close(index_add)
        }
    })
})

$('tbody').on("click", ".dele-btn", function () {
    let id = $(this).attr("data-id");
    console.log(id);
    layer.confirm('确认删除?', {
        icon: 3,
        title: '提示'
    }, function (index) {
        $.ajax({
            type:"get",
            url: "/my/article/deletecate/"+id,
            success:function(res){
                // console.log(res);
                 if (res.status !== 0) {
                     return layer.msg('删除分类失败！')
                 }
                 layer.msg('删除分类成功！')
                 console.log(1);
                 layer.close(index)
                 getArtData()
            }
        })
    })
})
let index_edit = null;
$("tbody").on("click", ".edit-btn",function(){
    let id = $(this).attr("data-id");
    if (id<2) return layer.msg("不能修改")
     index_edit = layer.open({
         type: 1,
         area: ['500px', '250px'],
         title: '修改文章分类',
         content: $("#edit_cont").html()
     })
     $.ajax({
         type:"get",
         url: "/my/article/cates/"+id,
         success:function(res){
             form.val("form-edit",res.data)
         }
     })
})
$("body").on("submit", "#form-edit", function (e) {
    e.preventDefault();
    $.ajax({
        type: "post",
        url: "/my/article/updatecate",
        data: $(this).serialize(),
        success: function (res) {
            console.log(res);
            if (res.status !== 0) {
                return layer.msg(res.message)
            }
            layer.msg(res.message);
            getArtData()
            layer.close(index_edit)
        }
    })
})